<?php

namespace Admin\Controller;

use Think\Controller;

class OverdueController extends CommonController
{
    public function __construct()
    {
        parent::__construct();
    }
    //逾期管理模块 首先展示
    public function overdue()
    {
        $this->display();
    }
    /*
    * 弹框添加备注
    * */
    public function add_mark()
    {
        $mark = I('info');
        $pay_no = I('number');
        if (empty(trim($mark))) {
            $this->ajax('1002', '信息填写完整！');
        }
        $arr = array('repayment_no' => $pay_no, 'reason' => $mark, 'name' => $_SESSION['name'], 'ctime' => time());
        if (M('mark', '', 'KEFU')->add($arr)) {
            $this->ajax('1001', '备注成功！');
        } else {
            $this->ajax('1002', '备注失败！');
        }

    }
    /*
     * 弹框备注
     * */
    public function overdue_mark()
    {
        $type = I('type');
//        $data = M('user_overdue', '', 'URGE')->where(['id' => $overdue_id])->field('repayment_no,status')->find();
//        $repay_no = $data['repayment_no'];
//        $this->status = $data['status'];
        $repay_no=I('repayment_no');
        $list = M('mark', '', 'KEFU')->where(['repayment_no' => $repay_no])->field('repayment_no,reason,name,ctime')->select();
        foreach ($list as $k => $v) {
            if ($v['ctime'] > 100) {
                $list[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
            } else {
                $list[$k]['ctime'] = '-';
            }
        }
        $this->assign(array(
            'data' => $list,
            'type' => $type,
            'number' => $repay_no,
            'repayment_no' => $repay_no,
        ));
        $this->display();
    }

    /**
     * 我跟进的逾期订单
     */
    public function myOverdueList()
    {
        $admin = $_SESSION['name'];
        $n = I('page', 1);//
        $num = 10;//一页几条
        $page = ($n - 1) * $num;
        $where = [];
        $where['a.name'] = array('eq', $admin);
        $my = M('mark', '', 'KEFU_R')->join('a join dxd_urge.user_overdue b on a.repayment_no=b.repayment_no')->where($where)->group('a.repayment_no')->field('a.id')->select();
        $count = count($my);
        $field = 'c.truename,c.sex,c.mobile,a.repayment_no,b.repayment_money,b.should_time,b.overdue_day,b.overdue_money,b.status';
        $data = M('mark', '', 'KEFU_R')->join('a join dxd_urge.user_overdue b on a.repayment_no=b.repayment_no')->join('dxd_user.user c on b.vid=c.vid')->where($where)->group('a.repayment_no')->limit($page, $num)->field($field)->select();

//        var_dump($data);die;

        foreach ($data as $k => $v) {
            $data[$k]['should_time'] = date('Y-m-d H:i', $v['should_time']);
            if ($v['sex'] == 1) {
                $data[$k]['sex'] = '男';
            } else if ($v['sex'] == 2) {
                $data[$k]['sex'] = '女';
            } else {
                $data[$k]['sex'] = '未知';
            }

            if ($v['status'] == 1) {
                $data[$k]['status'] = '未还款';
            } else if ($v['status'] == 2) {
                $data[$k]['status'] = '已还款';
            } else if($v['status'] == 3) {
                $data[$k]['status'] = '已续期';
            }
            //隐藏手机号
            $first=substr($v['mobile'],0,3);
            $end=substr($v['mobile'],7,4);
            $data[$k]['user_mobile']=$first.'*****'.$end;

        }


        $tol = ceil($count / $num);
        $this->ajaxReturn(['code' => '1001', 'total' => $tol, 'count' => $count, 'result' => $data]);


    }

    // 逾期列表
    public function OverdueList()
    {
        $type = I('type');//1未还款 2 已经还款
        $search = I('key');
        $n = I('page', 1);//
        $num = 10;//一页几条
        $page = ($n - 1) * $num;
        $overdue = M('user_overdue', '', 'URGE_R');

        $where = [];
        if (!empty($type) && $type != 3) {
            $where['b.status'] = array('eq', $type);
        }

        if (!empty($search)) {
            $where['a.truename|a.mobile'] = array('like', '%' . $search . '%');
        }
//        $where['b.merchant_num'] = array('eq', 'dh_kefu');
        $where['b.overdue_day'] = array('elt', 3);
        $where['b.type'] = array('eq', 1);
        $count = $overdue->join('b join dxd_user.user a on b.vid = a.vid')->where($where)->count();
        $field = 'b.id,a.truename,a.mobile,a.address,b.vid,b.repayment_no,b.repayment_money,b.should_time,b.overdue_day,b.overdue_money,b.overdue_time,b.status';
        $data = $overdue->join('b join dxd_user.user a on b.vid = a.vid')->order('b.id desc')->where($where)->LIMIT($page, $num)->field($field)->select();

        //已经转交的
        if ($type == 3) {
            $where = [];
            $where['a.status'] = array('neq', '3');
            $where['a.publish_merchant_num'] = array('eq', 'dh_kefu');

            if (!empty($search)) {
                $where['a.truename|a.mobile'] = array('like', '%' . $search . '%');
            }
            $field='b.id,b.vid,b.repayment_no,b.repayment_money,b.should_time,b.overdue_day,b.overdue_money,b.overdue_time,b.status';
            $count = $overdue->join('b join overdue_change_apply a on b.repayment_no=a.repayment_no')->where($where)->count();
            $data = $overdue->join('b join overdue_change_apply a on b.repayment_no=a.repayment_no')->limit($page, $num)->field($field)->where($where)->select();
        } else {

        }

        foreach ($data as $k => $v) {
            $user = M('user', '', 'USER')->where(['vid' => $v['vid']])->field('truename,mobile,address,sex')->find();
            $v['mobile']=$user['mobile'];
            if ($user['sex'] == 1) {
                $data[$k]['sex'] = '男';
            } else if ($user['sex'] == 2) {
                $data[$k]['sex'] = '女';
            } else {
                $data[$k]['sex'] = '未知';
            }
            $data[$k]['truename'] = $user['truename'];
            $data[$k]['mobile'] = $user['mobile'];
            $data[$k]['address'] = $user['address'];
            if ($v['status'] == 1) {
                $data[$k]['status'] = '未还款';
            } else if ($v['status'] == 2) {
                $data[$k]['status'] = '已还款';
            } else if ($v['status'] == 3) {
                $data[$k]['status'] = '已续期';
            }
            //最近备注
            $mark=M('mark','','KEFU_R')->where(['repayment_no'=>$v['repayment_no']])->field('reason,name')->order('id desc')->limit(1)->find();
            $data[$k]['mark_reason']=$mark['reason'];
            $data[$k]['admin_name']=$mark['name'];
            if(empty($mark)){
                $data[$k]['mark_reason']='--';
                $data[$k]['admin_name']='--';
            }




            $data[$k]['should_time'] = date('Y-m-d H:i', $v['should_time']);
            if (!empty($v['overdue_time'])) {
                $data[$k]['overdue_time'] = date('Y-m-d H:i', $v['overdue_time']);
            } else {
                $data[$k]['overdue_time'] = '-';
            }
            //判断是否转交过
            if ($type == 1) {
                $w = [];
                $w['repayment_no'] = array('eq', $v['repayment_no']);
                $w['status'] = array('neq', '3');
                $apply = M('overdue_change_apply', '', 'URGE')->where($w)->find();
                if (!empty($apply)) {
                    unset($data[$k]);
                }
            }
            //隐藏手机号
            $first=substr($v['mobile'],0,3);
            $end=substr($v['mobile'],7,4);
            $data[$k]['user_mobile']=$first.'*****'.$end;
        }
        $tol = ceil($count / $num);
        $this->ajaxReturn(['code' => '1001', 'total' => $tol, 'count' => $count, 'result' => $data]);
    }

    // 逾期已经移交列表
    public function Overdue_moveList()
    {
        $n = I('page', 1);//
        $num = 10;//一页几条
        $page = ($n - 1) * $num;
        $sub_sql=M('overdue_change_log','','URGE_R')->field('repayment_no,type')->group('repayment_no')->order('ctime desc')->select(false);
        $count=M('overdue_change_log','','URGE_R')->join('a join dxd_urge.user_overdue b on a.repayment_no=b.repayment_no')->count('DISTINCT a.repayment_no');
        $field='a.type,b.id,b.vid,b.repayment_no,b.repayment_money,b.should_time,b.overdue_day,b.overdue_money,b.overdue_time,b.status';
        $data=M('overdue_change_log','','URGE_R')->table('('.$sub_sql.') a')->join('join dxd_urge.user_overdue b on a.repayment_no=b.repayment_no')->field($field)->limit($page,$num)->select();

        foreach ($data as $k => $v) {
            $user = M('user', '', 'USER')->where(['vid' => $v['vid']])->field('truename,mobile,address,sex')->find();
            $v['mobile']=$user['mobile'];
            if ($user['sex'] == 1) {
                $data[$k]['sex'] = '男';
            } else if ($user['sex'] == 2) {
                $data[$k]['sex'] = '女';
            } else {
                $data[$k]['sex'] = '未知';
            }
            $data[$k]['truename'] = $user['truename'];
            $data[$k]['mobile'] = $user['mobile'];
            $data[$k]['address'] = $user['address'];
            if ($v['status'] == 1) {
                $data[$k]['status'] = '未还款';
            } else if ($v['status'] == 2) {
                $data[$k]['status'] = '已还款';
            } else if ($v['status'] == 3) {
                $data[$k]['status'] = '已续期';
            }else if ($v['status'] == 4) {
                $data[$k]['status'] = '已分期';
            }
            $data[$k]['should_time'] = date('Y-m-d H:i', $v['should_time']);
            if (!empty($v['overdue_time'])) {
                $data[$k]['overdue_time'] = date('Y-m-d H:i', $v['overdue_time']);
            } else {
                $data[$k]['overdue_time'] = '-';
            }
            //隐藏手机号
            $first=substr($v['mobile'],0,3);
            $end=substr($v['mobile'],7,4);
            $data[$k]['user_mobile']=$first.'*****'.$end;
        //    1、3天自动移交，2、申请移交，3人工移交，4、15天自动移交
            switch ($v['type']){
                case 1:$data[$k]['type']='3天自动移交';break;
                case 2:$data[$k]['type']='申请移交';break;
                case 3:$data[$k]['type']='人工移交';break;
                case 4:$data[$k]['type']='15天自动移交';break;
            }
        }

        $tol = ceil($count / $num);
        $this->ajaxReturn(['code' => '1001', 'total' => $tol, 'count' => $count, 'result' => $data]);
    }

    // 逾期已经移交列表
    public function Overdue_moveList1()
    {
      $type = I('get.type');//1未还款 2 已经还款
//        $search = I('get.key');
        $overdue = M('user_overdue', '', 'URGE');
        $row = '12';
        $where = [];


        $tol = $overdue->where($where)->count();
        $page = new PageController($tol, $row);
        $fpage = $page->fpage();

        //已经转交的
        if ($type == 3) {
            $tol = $overdue->where($where)->count();
            $where = [];
            $where['a.status'] = array('neq', '3');
            $where['a.publish_merchant_num'] = array('eq', 'dh_kefu');
            if (!empty($search)) {
                $where['a.truename|a.mobile'] = array('like', '%' . $search . '%');
            }
            $data = $overdue->join('b join overdue_change_apply a on b.repayment_no=a.repayment_no')->where($where)->select();

        }
        foreach ($data as $k => $v) {
            $user = M('user', '', 'USER')->where(['vid' => $v['vid']])->field('truename,mobile,address,sex')->find();
            if ($user['sex'] == 1) {
                $data[$k]['sex'] = '男';
            } else if ($user['sex'] == 2) {
                $data[$k]['sex'] = '女';
            } else {
                $data[$k]['sex'] = '未知';
            }
            $data[$k]['truename'] = $user['truename'];
            $data[$k]['mobile'] = $user['mobile'];
            $data[$k]['address'] = $user['address'];
            if ($v['status'] == 1) {
                $data[$k]['status'] = '未还款';
            } else if ($v['status'] == 2) {
                $data[$k]['status'] = '已还款';
            } else if ($v['status'] == 3) {
                $data[$k]['status'] = '已续期';
            }
            $data[$k]['should_time'] = date('Y-m-d H:i', $v['should_time']);
            if (!empty($v['overdue_time'])) {
                $data[$k]['overdue_time'] = date('Y-m-d H:i', $v['overdue_time']);
            } else {
                $data[$k]['overdue_time'] = '-';
            }

            //判断是否转交过
            if ($type == 1) {
                $w = [];
                $w['repayment_no'] = array('eq', $v['repayment_no']);
                $w['status'] = array('neq', '3');
                $apply = M('overdue_change_apply', '', 'URGE')->where($w)->find();
                if (!empty($apply)) {
                    unset($data[$k]);
                }
            }
        }
        $tol = count($data);

        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'type' => $type,
            'tol' => $tol
        ));
        $this->display();
    }

    // 移交列表
    public function OverdueApplyRecord()
    {
        $apply = M('overdue_change_apply', '', 'URGE_R');
        $row = '12';
        $where = [];
        $search = I('get.key');
        $this->key = $search;
        if (!empty($search)) {
            $where['c.truename|c.mobile'] = array('like', '%' . $search . '%');
        }
        $where['publish_merchant_num'] = array('eq', 'dh_kefu');
        $tol = $apply->join('b join urge_merchant a on b.accept_merchant_num = a.merchant_num')
            ->join('user_overdue d on b.repayment_no=d.repayment_no ')->join('dxd_user.user c on d.vid=c.vid')
            ->order('b.ctime desc')->where($where)->count();

        $page = new PageController($tol, $row);
        $fpage = $page->fpage();
        $field = 'b.repayment_no,b.ctime,b.status,a.name,c.truename,c.mobile';
        $data = $apply->join('b join urge_merchant a on b.accept_merchant_num = a.merchant_num')
            ->join('user_overdue d on b.repayment_no=d.repayment_no ')->join('dxd_user.user c on d.vid=c.vid')
            ->order('b.ctime desc')->where($where)->LIMIT($page->listfirst, $page->listRows)->field($field)->select();

        $faqi = M('urge_merchant', '', 'URGE')->where(['merchant_num' => 'dh_kefu'])->getfield('name');
        foreach ($data as $k => $v) {
            if ($v['status'] == 1) {
                $data[$k]['status'] = '待处理';
            } else if ($v['status'] == 2) {
                $data[$k]['status'] = '接收';
            } else if ($v['status'] == 3) {
                $data[$k]['status'] = '拒绝';
            } else if ($v['status'] == 3) {
                $data[$k]['status'] = '自动移交';
            }
            $data[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);

        }
        $this->assign(array(
            'data' => $data,
            'fpage' => $fpage,
            'faqi' => $faqi,
            'tol' => $tol
        ));
        $this->display();

    }


    // 转交
    public function Overdue_apply()
    {
        if (empty($_POST)) {
            $overdue = I('get.id');
            $w = [];
            $w['status'] = array('eq', '1');
            $w['merchant_num'] = array('neq', 'dh_kefu');
            //如果是第一次移交 必须选择北京新贷后
            $first = M('user_overdue', '', 'URGE')->join('b join overdue_change_apply a on b.repayment_no=a.repayment_no')->where(['b.id' => $overdue])->find();
            if (empty($first)) {
                $w['merchant_num'] = array('eq', 'dh_12345678');
            }
            $merchant = M('urge_merchant', '', 'URGE')->where($w)->field('id,name')->select();

            $this->assign(array(
                'merchant' => $merchant,
                'overId' => $overdue
            ));
            $this->display();
            die;
        } else {

            $id = I('id');
            $overdue = I('overdue');
            $borrow = M('user_overdue', '', 'URGE')->where(['id' => $overdue])->field('repayment_no,overdue_day')->find();//订单号\
            if ($borrow['overdue_day'] <= 3) {
                $this->ajax('1002', '逾期不超过3天！');
            }

            //是否已经移交
            $exit = M('overdue_change_apply', '', 'URGE')->where(['repayment_no' => $borrow['repayment_no']])->find();
            if (!empty($exit)) {
                $this->ajax('1002', '转交已存在');
            }

            $accept_num = M('urge_merchant', '', 'URGE')->where(['id' => $id])->getfield('merchant_num');//贷后公司码

            $arr = array('publish_merchant_num' => 'dh_kefu', 'repayment_no' => $borrow['repayment_no'], 'ctime' => time(), 'accept_merchant_num' => $accept_num);
            $add = M('overdue_change_apply', '', 'URGE')->add($arr);
            if ($add) {
                $this->ajax('1001', '申请成功');
            } else {
                $this->ajax('1002', '申请失败');
            }
        }
    }

    /**
     * 备注显示
     */
    public function mark_show(){
        $repayment_no=I('repayment_no');
        $list=M('mark', '', 'KEFU')->where(['repayment_no'=>$repayment_no])->field('repayment_no,reason,name,ctime')->select();
    }

    /**
     * 外呼不成功 逾期提醒
     */
    public function overdue_mention(){
        $n = I('page', 1);//
        $num = 10;//一页几条
        $page = ($n - 1) * $num;
        $where['c.outcome']=['eq','SUCCESS'];
        $where['a.status']=1;
        $where['a.overdue_day']=['elt',7];
        $field = 'b.truename,b.sex,b.mobile,a.repayment_no,a.repayment_money,a.should_time,a.overdue_day,a.overdue_money,a.status';
        $count=M('user_overdue','','URGE')->join('a join dxd_user.user b on b.vid=a.vid and b.type!=2')->join(' join dxd_kefu.ivr_callback_record c on c.phone=b.mobile and c.type=4')->where($where)->count();
        $data=M('user_overdue','','URGE')->join('a join dxd_user.user b on b.vid=a.vid and b.type!=2')->join('join dxd_kefu.ivr_callback_record c on c.phone=b.mobile and c.type=4')->field($field)->limit($page,$num)->where($where)->select();

        foreach ($data as $k => $v) {
            if ($v['sex'] == 1) {
                $data[$k]['sex'] = '男';
            } else if ($v['sex'] == 2) {
                $data[$k]['sex'] = '女';
            } else {
                $data[$k]['sex'] = '未知';
            }
            $data[$k]['truename'] = $v['truename'];
            $data[$k]['mobile'] = $v['mobile'];
            if ($v['status'] == 1) {
                $data[$k]['status'] = '未还款';
            } else if ($v['status'] == 2) {
                $data[$k]['status'] = '已还款';
            } else if ($v['status'] == 3) {
                $data[$k]['status'] = '已续期';
            }
            $data[$k]['should_time'] = date('Y-m-d H:i', $v['should_time']);
            if (!empty($v['overdue_time'])) {
                $data[$k]['overdue_time'] = date('Y-m-d H:i', $v['overdue_time']);
            } else {
                $data[$k]['overdue_time'] = '-';
            }
            //最近备注
            $mark=M('mark','','KEFU_R')->where(['repayment_no'=>$v['repayment_no']])->field('reason,name')->order('id desc')->limit(1)->find();
            $data[$k]['mark_reason']=$mark['reason'];
            $data[$k]['admin_name']=$mark['name'];
            if(empty($mark)){
                $data[$k]['mark_reason']='--';
                $data[$k]['admin_name']='--';
            }

            //隐藏手机号
            $first=substr($v['mobile'],0,3);
            $end=substr($v['mobile'],7,4);
            $data[$k]['user_mobile']=$first.'*****'.$end;
        }
        $tol = ceil($count / $num);
        $this->ajaxReturn(['code' => '1001', 'total' => $tol, 'count' => $count, 'result' => $data]);
    }


}